package com.example.demo3.controller;


import com.example.demo3.entity.User;
import com.example.demo3.service.UserService;
import com.example.demo3.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UserController {

    @Autowired
    UserService userService;


    @ApiOperation("测试方法")
    @RequestMapping("/select")
    public List<User> select() {
        return userService.select();
    }

    @ApiOperation("登录接口")
    @RequestMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @RequestMapping("/selectAccount")
    @ApiOperation("查询用户是否存在")
    public Result getAccount(String userAccount) {
        return userService.selectAccount(userAccount);
    }

    @ApiOperation("注册")
    @GetMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    @ApiOperation("退出")
    @GetMapping("/exit")
    public Result exit(HttpServletRequest rq) {
        return userService.exit(rq.getHeader("token"));
    }

    @ApiOperation("分页查询用户，条件：名称模糊查询;账号模糊查询;手机号查询")
    @RequestMapping("/selectList")
    public Result selectList(User user) {
        return userService.selectList(user);
    }

    @ApiOperation("新增用户")
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        return  userService.add(user);
    }

    @ApiOperation("id删除用户")
    @RequestMapping("/delete")
    public Result delete(Integer userId){
        return userService.delect(userId);
    }

    @ApiOperation("修改用户")
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateUser(user);
    }
}
